/*******************************************************************
 *   > File Name: wdt.c
 *   > Author: fly
 *   > Mail: XXXXXXXX@icode.com
 *   > Create Time: Sat 07 Mar 2020 07:20:40 PM PST
 ******************************************************************/
#include "int.h"

#define WTCON       0xE2700000
#define WTDAT       0xE2700004
#define WTCNT       0xE2700008
#define WTCLRINT    0xE270000C

#define rWTCON      (*(volatile unsigned *)WTCON)
#define rWTDAT      (*(volatile unsigned *)WTDAT)
#define rWTCNT      (*(volatile unsigned *)WTCNT)
#define rWTCLRINT   (*(volatile unsigned *)WTCLRINT)

void wdt_init_interrupt(void)
{
    //1.设置好预分频器和分频器
    rWTCON &= ~(0xFF<<8);
    rWTCON |= (65<<8);//1Mhz
    
    rWTCON |= ~(3<<3);
    rWTCON &= (3<<3);//1/128

    //2.设置中断和复位信号的使能
    rWTCON |= (1<<2);//enable wdt interrupt
    rWTCON &= ~(1<<0);//disable wdt reset

    //3.设置定时时间
    rWTDAT = 1000;//定时1.28s

    //4.设置完，开看门狗
    rWTCON |= (1<<5);
}

//看门狗中断处理程序
void isr_wdt(void)
{
    static int i= 0;
    printf("wdt interrupt, i = %d...\r\n", i++);

    intc_clearvectaddr();
    rWTCLRINT = 1;
}
